package com.example.dishfo.myapplication.customview.toolbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dishfo.myapplication.R;

public class TextTitle extends RelativeLayout{

    private TextView textView_title;
    private ImageButton button;

    public TextTitle(Context context) {
        this(context,null,0,0);
    }

    public TextTitle(Context context, AttributeSet attrs) {
        this(context, attrs,0,0);
    }

    public TextTitle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);

    }

    public TextTitle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        LayoutInflater.from(context).inflate(R.layout.toolbar_withback,this);
        textView_title=findViewById(R.id.toolbar_title);
        button=findViewById(R.id.toolbar_button);

        TypedArray a=context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.TextTitle,defStyleAttr,0);

        Drawable background=null;

        int n=a.getIndexCount();
        for (int i=0;i<n;i++){
            switch (a.getIndex(i)){
                case R.styleable.TextTitle_title:
                    String title=a.getString(a.getIndex(i));
                    textView_title.setText(title);
                    break;
                case R.styleable.TextTitle_button_image:
                    Drawable drawable=a.getDrawable(a.getIndex(i));
                    button.setBackground(drawable);
                    break;
                case R.styleable.TextTitle_text_color:
                    int color =a.getColor(a.getIndex(i), Color.WHITE);
                    textView_title.setTextColor(color);
                    break;
                case R.styleable.TextTitle_title_background:
                    background=a.getDrawable(a.getIndex(i));
                    break;
            }
        }

        a.recycle();
        if (background!=null){
            background=getContext().getTheme().getDrawable(android.R.color.transparent);
            this.setBackground(background);
        }
    }

    public void setOnClickListener(OnClickListener listener){
        button.setOnClickListener(listener);
    }

    public void setTitle(String title){
        this.textView_title.setText(title);
    }


}
